//
//  ZCMeNotificationCenterVC.m
//  zctx
//
//  Created by apple on 15/11/5.
//  Copyright © 2015年 sanliang. All rights reserved.
//

#import "ZCMeNotificationCenterVC.h"
#import "ZCMeNotificationCenterCell.h"
#import "ZCMeAuditDetailsVC.h"  //审核中
#import "ZCSamplingDetailVC.h"  //抽样中
#import "ZCInvolvedDetailsVC.h"  //众筹中
#import "ZCProjectdetailsVC.h"  //结果

#import "DZWebViewController.h"

@interface ZCMeNotificationCenterVC ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)UITableView *tableView;
@end

@implementation ZCMeNotificationCenterVC


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"ZCMeNotificationCenterVC"];
    [MTA trackPageViewBegin:@"ZCMeNotificationCenterVC"];
    self.tabBarController.hidesBottomBarWhenPushed = YES;
    
//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//        for (NSDictionary *dict in self.dataArray) {
//            NSString *isread = [NSString stringWithFormat:@"%@",[dict objectForKey:@"isread"]];
//            isread = @"1";
//            [dict setValue:isread forKey:@"isread"];
//        }
//    });
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"ZCMeNotificationCenterVC"];
    [MTA trackPageViewEnd:@"ZCMeNotificationCenterVC"];
    self.tabBarController.hidesBottomBarWhenPushed = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#ebeded"];
    [self.navigationItem setTitle:@"我的消息"];
    [self initnavegation];
    [self initTableView];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCMeNotificationCenterCell" bundle:nil] forCellReuseIdentifier:@"centercell"];
}

#pragma mark 导航栏
-(void)initnavegation{
    UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 19, 36);
    [leftButton setImage:[UIImage imageNamed:@"navigation_back"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * barButtonItem_leftNavBar = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = barButtonItem_leftNavBar;
}

-(void)backButtonPressed{

    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark 请求结果数据
//-(void)requestSeeResultdata{
//    
//    [GMDCircleLoader setOnView:self.view withTitle:@"努力加载中..." animated:YES];
//    [NetworkManager requestMemberMessageWithMid:UserModel.mid password:UserModel.password success:^(NSDictionary *ArrayResponse) {
//        [GMDCircleLoader hideFromView:self.view animated:YES];
//        if ([[ArrayResponse objectForKey:@"content"] isKindOfClass:[NSArray class]]) {
//            NSArray *dict = (NSArray *)[ArrayResponse objectForKey:@"content"];
//            self.dataArray = [dict mutableCopy];
//            [self.tableView reloadData];
//        }
//    } failure:^(NSError *error) {
//        [GMDCircleLoader hideFromView:self.view animated:YES];
//    }];
//     
//}

-(void)initTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.translatesAutoresizingMaskIntoConstraints = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.tableView.sectionHeaderHeight = 0.0001;
    self.tableView.separatorColor = RGB(236, 237, 238);
    self.tableView.backgroundColor = [UIColor colorWithHexString:@"#fafcfb"];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0,0,0));
    }];
}

#pragma mark UITableViewDataSource,UITableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellid =@"centercell";
    ZCMeNotificationCenterCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    if (cell == nil) {
        cell = [[ZCMeNotificationCenterCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *dict = (NSDictionary *)self.dataArray[indexPath.row];
    cell.contentLable.text = [NSString stringWithFormat:@"%@",[dict objectForKey:@"text"]];
   int time = [NSString stringWithFormat:@"%@",[dict objectForKey:@"ctime"]].intValue;
   cell.timeLable.text =[NSString stringWithFormat:@"%@",dateTimeWithNString(time)];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    /*
    
    NSDictionary *dict = (NSDictionary *)self.dataArray[indexPath.row];
    NSString *type =[NSString stringWithFormat:@"%@",[dict objectForKey:@"showtype"]];
    NSString *state =[NSString stringWithFormat:@"%@",[dict objectForKey:@"fd_state"]];
    NSString *fdid =[NSString stringWithFormat:@"%@",[dict objectForKey:@"fdid"]];
    NSString *title =[NSString stringWithFormat:@"%@",[dict objectForKey:@"title"]];
    NSString *content =[NSString stringWithFormat:@"%@",[dict objectForKey:@"content"]];

//    NSLog(@"type = %@ , %@ ,%@ ",type,state,fdid);
    if ([type isEqualToString:@"1"]) {
        //项目消息
        if ([state isEqualToString:@"0"])
        { // 0 审核中 1众筹中 2 抽样中 3完成 4 异常
           
            ZCMeAuditDetailsVC *vc = [[ZCMeAuditDetailsVC alloc]init];
            vc.pid =fdid;
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }else if ([state isEqualToString:@"1"]){
            ZCInvolvedDetailsVC *vc = [[ZCInvolvedDetailsVC alloc]init];
            vc.pid =fdid;
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }else if ([state isEqualToString:@"2"]){
            ZCSamplingDetailVC *vc = [[ZCSamplingDetailVC alloc]init];
            vc.pid =fdid;
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }else if ([state isEqualToString:@"3"]){
            ZCProjectdetailsVC *vc = [[ZCProjectdetailsVC alloc]init];
            vc.pid =fdid;
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }else{
        //系统消息
        DZWebViewController *webvc = [[DZWebViewController alloc]init];
        webvc.strURL = content;
        webvc.title = title;
        webvc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:webvc animated:YES];
    }
     */
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dict = (NSDictionary *)self.dataArray[indexPath.row];
    NSString *contentStr = [NSString stringWithFormat:@"%@",[dict objectForKey:@"text"]];
    //    NSLog(@"model = %@",model);
    NSString *reuseIdentifier = @"centercell";
    ZCMeNotificationCenterCell *cell = [self.tableView dequeueReusableCellWithIdentifier:  reuseIdentifier];
    [cell.contentLable setText:contentStr];
    
    [cell setNeedsUpdateConstraints];
    [cell updateConstraintsIfNeeded];
    // 得到cell的contentView需要的真实高度
    CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    
    //    NSLog(@"heithy = %f",height);
    // 要为cell的分割线加上额外的1pt高度。因为分隔线是被加在cell底边和contentView底边之间的。
    height +=1.0f;
    return height;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    if (self.dataArray.count>0) {
        UIView *footView = [[UIView alloc]init];
        footView.backgroundColor = [UIColor colorWithHexString:@"#fafcfb"];
        return footView;
    }else{
        
        UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH,400)];
        footView.backgroundColor = [UIColor colorWithHexString:@"#fafcfb"];
        
        UIImageView*redImg = [[UIImageView alloc]init];
        [footView addSubview:redImg];
        [redImg makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(footView.top).with.offset(0.255 * SCREEN_HEIGHT);
            make.centerX.equalTo (footView.centerX).with.offset(0);
            make.height.mas_equalTo(100);
            make.width.mas_equalTo(100);
        }];
        [redImg setImage:[UIImage imageNamed:@"me_notification_no"]];
        
        UILabel*redLable = [[UILabel alloc]init];
        [footView addSubview:redLable];
        [redLable makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(redImg.bottom).with.offset(24);
            make.centerX.equalTo (footView.centerX).with.offset(0);
            make.height.mas_equalTo(21);
            make.width.mas_equalTo(SCREEN_WIDTH-20);
        }];
        redLable.textAlignment = NSTextAlignmentCenter;
        redLable.text = @"暂时还没有收到系统消息";
        redLable.textColor = [UIColor colorWithHexString:@"#d2d2d2"];
        redLable.font = [UIFont systemFontOfSize:16];
        return footView;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (self.dataArray.count>0) {
        return 0.001;
    }else{
        return 400;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
